package com.fdb.efp.riskm.api.rest;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.fdb.efp.ctr.service.facade.CtrLoanContService;
import com.fdb.efp.ctr.service.vo.CtrLoanContVO;
import com.fdb.efp.cus.service.facade.CusIndivService;
import com.fdb.efp.cus.service.vo.CusIndivVO;
import com.fdb.efp.e4a.service.facade.SRoleDataRuleService;
import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.core.util.ReflectUtility;
import com.fdb.basic.framework.web.controller.AbstractController;
import com.fdb.efp.loan.service.facade.AccLoanService;
import com.fdb.efp.riskm.service.facade.RscAdjustAppService;
import com.fdb.efp.riskm.service.facade.RscAdjustBillService;
import com.fdb.efp.riskm.service.facade.RscAdjustContService;
import com.fdb.efp.riskm.service.vo.RscAdjustAppVO;
import com.fdb.efp.riskm.service.vo.RscAdjustContVO;

@RestController
@RequestMapping("/api")
public class RscAdjustContController extends AbstractController implements  FrameworkService {
	
//	@Autowired
//	@Qualifier("rscAdjustContService")
//	private RscAdjustContService rscAdjustContService;
//	@Autowired
//	@Qualifier("ctrLoanContService")
//	private CtrLoanContService ctrLoanContService;
//	@Autowired
//	@Qualifier("rscAdjustBillService")
//	private RscAdjustBillService rscAdjustBillService;
//	@Autowired
//	@Qualifier("accLoanService")//DEL
//	private AccLoanService accLoanService;
//	@Autowired
//	@Qualifier("cusIndivService")
//	private CusIndivService cusIndivService;
//	@Autowired
//	@Qualifier("rscAdjustAppService")
//	private RscAdjustAppService rscAdjustAppService;
//	@Autowired
//	@Qualifier("sRoleDataRuleService")
//	private SRoleDataRuleService sRoleDataRuleService;
//
//	/**
//	 * 列表数据查询
//	 * @param page
//	 * @param size
//	 * @return
//	 */
//	@RequestMapping(value="/rsc/adjust/conts", method=RequestMethod.POST)
//	public @ResponseBody ResponseData<List<RscAdjustContVO>> queryRscAdjustContAll(
//			@RequestBody RscAdjustContVO vo) {		
////		List<RscAdjustContVO> outputVo = this.executeQueryList("queryAll", rscAdjustContService, new Object[]{vo});
//		/*List<RscAdjustContVO> outputVo = rscAdjustContService.queryAllOwner(vo);
//		return getResponseData(outputVo);*/
//		setUserInfoToVO(vo);
//		// 根据用户角色获取用户权限方案
//		String dataAuthority = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
//		List<RscAdjustContVO> outputVo = null;
//		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataAuthority);
//
//		// 根据权限方案调用不同的方案进行数据查询
//		switch (dataRuleType) {
//		case Owner:
//			outputVo = rscAdjustContService.queryAllOwner(vo);
//			break;
//		case CurrOrg:
//			outputVo = rscAdjustContService.queryAllCurrOrg(vo);
//			break;
//		case CurrDownOrg:
//			outputVo = rscAdjustContService.queryAllCurrDownOrg(vo);
//			break;
//		default:
//			outputVo = rscAdjustContService.queryAllOwner(vo);
//			break;
//		}
//		return getResponseData(outputVo);
//		
//	}
//	
//	/**
//	 * 根据主键查询详情
//	 * @return
//	 */
//	@RequestMapping(value="/rsc/adjust/cont/{taskNo}{contNo}/", method=RequestMethod.GET)
//	public @ResponseBody ResponseData<RscAdjustContVO> queryByPk(@PathVariable("taskNo") String taskNo,@PathVariable("contNo") String contNo) {		
//		RscAdjustContVO inVo = new RscAdjustContVO();
//				inVo.setTaskNo(taskNo);
//				inVo.setContNo(contNo);
//		RscAdjustContVO outputVo = rscAdjustContService.queryByPk(inVo);
//		return getResponseData(outputVo);
//		
//	}
//	
//	/**
//	 * 根据客户号和担保方式删除信息
//	 * @param rscAdjustCont
//	 * @return
//	 */
//	@RequestMapping(value="/rsc/adjust/cont", method=RequestMethod.DELETE)
//	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody RscAdjustContVO inRscAdjustContVo) {		
//		int outputVo = rscAdjustContService.deleteByPk(inRscAdjustContVo);
//		return getResponseData(outputVo);
//		
//	}
//	
//	/**
//	 * 根据客户号，担保方式进行查询
//	 * @param page
//	 * @param size
//	 * @return
//	 */
//	@RequestMapping(value="/rsc/adjust/cont/CusId", method=RequestMethod.POST)
//	public @ResponseBody ResponseData<List<RscAdjustContVO>> queryAllOwnerByPageCusId(@RequestBody RscAdjustContVO vo) {		
//		List<RscAdjustContVO> outputVo = rscAdjustContService.queryAllOwnerByPageCusId(vo);
//		return getResponseData(outputVo);
//		
//	}
//	/**
//	 * 根据主键更新信息
//	 * @param inRscAdjustContVo
//	 * @return
//	 */
//	@RequestMapping(value="/rsc/adjust/cont", method=RequestMethod.PUT)
//	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody RscAdjustContVO inRscAdjustContVo) {	
//		int outputVo = rscAdjustContService.updateByPk(inRscAdjustContVo);
//		return getResponseData(outputVo);
//	}
//	
//	/**
//	 * 新增数据
//	 * @param inRscAdjustContVo
//	 * @return
//	 */
//	@RequestMapping(value="/rsc/adjust/cont", method=RequestMethod.POST)
//	public @ResponseBody ResponseData<Integer> insertRscAdjustCont(@RequestBody RscAdjustContVO inRscAdjustContVo) {
//		int num = 0;
//		int num2=0;
//		logger.info("按合同批量插入入参："+JSONObject.toJSONString(inRscAdjustContVo));
//		RscAdjustAppVO rscAdjustApp = new RscAdjustAppVO();
//		beanCopy(inRscAdjustContVo, rscAdjustApp);
//		
//		String contNo=inRscAdjustContVo.getContNo();
//		List<String> contNoList = null;
//		//判断contNo是否为空
//		if(contNo!=null && !"".equals(contNo)) {
//			String[] contno=contNo.split(",");
//			contNoList=Arrays.asList(contno);
//		}
//		logger.info("合同集合参数为："+JSONObject.toJSONString(contNoList));
//		CusIndivVO cusIndivVO =new CusIndivVO();
//		List<RscAdjustContVO> rscAdjustContList = new ArrayList<RscAdjustContVO>();
//		List<CtrLoanContVO> list = ctrLoanContService.queryCtrLoanContListByContNos(contNoList);
//		for(int i=0; i<list.size();i++) {
//			CtrLoanContVO ctrLoanContVo =list.get(i);
//			RscAdjustContVO RscAdjustContVo = new RscAdjustContVO();
//			beanCopy(ctrLoanContVo, RscAdjustContVo);
//			RscAdjustContVo.setTaskNo(inRscAdjustContVo.getTaskNo());
//			RscAdjustContVo.setClaDate(null);
//			cusIndivVO.setCusId(RscAdjustContVo.getCusId());
//			CusIndivVO queryRslCusIndiv = cusIndivService.queryByPk(cusIndivVO);
//			logger.info("根据客户号："+RscAdjustContVo.getCusId()+"查询到的客户信息为："+JSONObject.toJSONString(queryRslCusIndiv));
//			
//			if(Objects.nonNull(queryRslCusIndiv)) {
//				RscAdjustContVo.setCusCdt(queryRslCusIndiv.getCrdGrade());
//				RscAdjustContVo.setIsAgri(queryRslCusIndiv.getAgriFlg());
//			}
//			RscAdjustContVO outputVo = rscAdjustContService.queryByPk(RscAdjustContVo);
//			if(Objects.nonNull(outputVo)) {
//				num = rscAdjustContService.deleteByPk(RscAdjustContVo);
//			}
//			logger.info("任务编号："+RscAdjustContVo.getTaskNo()+"的风险分类调整合同参数为："+JSONObject.toJSONString(RscAdjustContVo));
//			rscAdjustContList.add(RscAdjustContVo);
//		}
//		if (Objects.nonNull(rscAdjustContList) && rscAdjustContList.size() > 0) {
//			logger.info("批量插入合同条数为："+rscAdjustContList.size());
//			logger.info("批量插入合同信息为："+JSONObject.toJSONString(rscAdjustContList));
//			num = rscAdjustContService.insertRscAdjustCont(rscAdjustContList);
//		}
//		logger.info("rscAdjustApp信息为："+JSONObject.toJSONString(rscAdjustApp));
//		num2 = rscAdjustAppService.updateByPk(rscAdjustApp);
//		if(num2 != 1) {
//			logger.error("对主表进行修改出错！");
//		}
//		return getResponseData(num);
//		
//	}
//	
//	/**
//	 * 调用合同，客户
//	 * @param inRscAdjustContVo
//	 * @return
//	 */
//	@RequestMapping(value="/rsc/adjust/cont/crt", method=RequestMethod.POST)
//	public @ResponseBody ResponseData<List<RscAdjustContVO>> queryCrtCus(@RequestBody RscAdjustContVO RscAdjustContVo) {
//		setUserInfoToVO(RscAdjustContVo);
//		CtrLoanContVO ctrLoanContVO = new CtrLoanContVO();
//		//判断页面是否输入查询条件，如果不输入，页面不显示数据
//		beanCopy(RscAdjustContVo, ctrLoanContVO);
//		logger.info("ctrLoanContVO对象信息为："+JSONObject.toJSONString(ctrLoanContVO));
//		CusIndivVO cusIndivVO =new CusIndivVO();
//		if(!allFieldsIsNull(ctrLoanContVO)) {
//			List<RscAdjustContVO> lists = new ArrayList<RscAdjustContVO>();
//			//返回未结清借据对应的合同号
//			List<String> contNos=accLoanService.queryContListByAccStatus();
//			if(Objects.isNull(contNos) || contNos.size()==0) {
//				return null;
//			}
//			logger.info("未结清借据对应的合同号为："+JSONObject.toJSONString(contNos));
//			
//			List<String> rscAdjustContNo = new CopyOnWriteArrayList<>(contNos);// 临时变量存储
//			logger.info("临时变量存储信息:" + JSONObject.toJSONString(rscAdjustContNo) );
//			if (Objects.nonNull(rscAdjustContNo)) {
//				List<String> rscAdjustCont = rscAdjustContService.queryContNo();
//				logger.info("判断该合同是否有借据处于待发起、审批中："+JSONObject.toJSONString(rscAdjustCont));
//				rscAdjustContNo.removeAll(rscAdjustCont);
//				logger.info("移除相同合同号后："+JSONObject.toJSONString(rscAdjustContNo));
//			}
//			
//			//根据合同号查出合同列表
//			logger.info("移除相同合同号后数量："+rscAdjustContNo.size());
//			ctrLoanContVO.setContNos(rscAdjustContNo);
//			logger.info("移除相同合同后信息："+JSONObject.toJSONString(ctrLoanContVO));
////			List<CtrLoanContVO> list = ctrLoanContService.queryByConditionByPage(ctrLoanContVO);
//			
//			//List<CtrLoanContVO> list = this.executeQueryList("queryBy", ctrLoanContService, new Object[]{ctrLoanContVO});
//			
//			// 根据用户角色获取用户权限方案
//			String dataAuthority = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
//			List<CtrLoanContVO> list = null;
//			DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataAuthority);
//
//			// 根据权限方案调用不同的方案进行数据查询
//			switch (dataRuleType) {
//			case Owner:
//				list = ctrLoanContService.queryAllOwner(ctrLoanContVO);
//				break;
//			case CurrOrg:
//				list = ctrLoanContService.queryAllCurrOrg(ctrLoanContVO);
//				break;
//			case CurrDownOrg:
//				list = ctrLoanContService.queryAllCurrDownOrg(ctrLoanContVO);
//				break;
//			default:
//				list = ctrLoanContService.queryAllOwner(ctrLoanContVO);
//				break;
//			}
//			//return getResponseData(list);
//			if(Objects.isNull(list) || list.size()==0) {
//				return null;
//			}
//			for(CtrLoanContVO ctrLoanContVo : list) {
//				RscAdjustContVO inRscAdjustContVo = new RscAdjustContVO();
//				beanCopy(ctrLoanContVo, inRscAdjustContVo);
////				List<RscAdjustContVO> rscAdjustCont = rscAdjustContService.queryContNo(inRscAdjustContVo);
////				logger.info("判断该合同是否有借据处于待发起、审批中："+JSONObject.toJSONString(rscAdjustCont));
////				if(Objects.isNull(rscAdjustCont) || rscAdjustCont.size()==0) {
//					
//					inRscAdjustContVo.setContNo(ctrLoanContVo.getContNo());
//					inRscAdjustContVo.setContAmt(ctrLoanContVo.getContAmt());
//					inRscAdjustContVo.setPrdCode(ctrLoanContVo.getPrdCode());
//					inRscAdjustContVo.setPrdName(ctrLoanContVo.getPrdName());
//					inRscAdjustContVo.setAssureMeansMain(ctrLoanContVo.getAssureMeansMain());
//					
//					cusIndivVO.setCusId(ctrLoanContVo.getCusId());
//					CusIndivVO queryRslCusIndiv = cusIndivService.queryByPk(cusIndivVO);
//					if(Objects.nonNull(queryRslCusIndiv)) {
//						inRscAdjustContVo.setCusCdt(queryRslCusIndiv.getCrdGrade());
//						inRscAdjustContVo.setIsAgri(queryRslCusIndiv.getAgriFlg());
//					}
//					logger.info("inRscAdjustContVo信息为："+JSONObject.toJSONString(inRscAdjustContVo));
//					lists.add(inRscAdjustContVo);
////				}
//			}
//			return getResponseData(lists);
//		}else {
//			return null;
//		}
//	}
//	
//	//判断前端是否有参数传入，进行条件搜索
//	public static boolean allFieldsIsNull(Object obj) {
//		if (null == obj)
//			return true;
//		Field[] fields = obj.getClass().getDeclaredFields();
//		Object value;
//		for (int i = 0; i < fields.length; i++) {
//			try {
//				value = ReflectUtility.getObjectFieldValue(obj, fields[i]);
//				if ( value == null)
//					continue;
//				if (value instanceof String ) {
//					String s = (String) value;
//					if ( "".equals(s.trim()) )	
//					 continue;
//				}
//				if (value instanceof List ) {
//					List<?> list = (List<?>) value;
//					if (list.size() == 0 )
//						continue;
//					int num = 0; //空值的次数
//					for (Object object : list) {
//						if (object == null)
//							num++;
//						if (object instanceof String ) {
//							String s = (String) object;
//							if ( "".equals(s.trim()) )	
//								num++;
//						}
//					}
//					if ( num == list.size())
//						continue;
//				}
//				if ( value.getClass().isPrimitive() ) 
//					continue;
//				if ("serialVersionUID".equals(fields[i].getName()))
//					continue;
//				return false;
//			} catch (IllegalArgumentException e) {
//			} 
//		}
//		return true;
//	}
	
}
